"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 文件目录
Case Name   : 主机实例数据目录属组正确权限修改恢复
Create At   : 2020/07/23
Owner       : @li-xin12345
Description :
    1. 修改主机数据库实例目录权限
    2. 重启数据库主机
    3. 备机1failover、refreshconf
    4. kill主机数据库进程
    5. 恢复主机数据库实例目录权限
    6. 重启数据库
    7. 新主机执行tpcc
Expect      :
    1. 修改主机数据库实例目录权限成功
    2. 重启数据库主机失败
    3. 备机1failover成功、refreshconf提示主节点失败，实际动态文件已刷新
    4. kill主机数据库进程成功
    5. 恢复主机数据库实例目录权限成功
    6. 重启数据库成功
    7. 新主机执行tpcc成功
History     :
    modified：2021-4-22 by 5328113;状态结果查询方式变更，修改脚本；
    modified：2021-3-24 by 5328113;修改主机dn1目录权限后，refreshconf返回结果原主机节点失败
        （提示permission问题)，但是动态文件实际已刷新成功；新增了正常校验逻辑；需要修改断言
    modified by @wan005 at 2022/01/21:优化用例去除-R
"""

import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class IndexFileDamaged(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case182.py start----')
        self.pri_root = Node('PrimaryRoot')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.sta_root = Node(node='Standby1Root')
        self.sta_dbuser = Node(node='Standby1DbUser')
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.com = Common('Standby1Root')
        self.node_num = self.com.get_node_num(self.pri_root)
        self.log.info(self.node_num)
        self.min_time = 300

    def test_Index_file_damaged(self):
        self.log.info('----修改主机数据库实例目录权限----')
        excute_cmd = f'chmod 000 {macro.DB_INSTANCE_PATH}'
        self.log.info(excute_cmd)
        msg = self.pri_dbuser.sh(excute_cmd).result()
        self.log.info(msg)
        self.assertNotIn(self.constant.NO_FILE_MSG, msg)

        self.log.info('----重启主节点----')
        result = self.pri_sh.stopDbInstance()
        self.assertIn(self.constant.PERMISSION_DENIED, result)
        result = self.pri_sh.getDbClusterStatus('detail')
        self.log.info(result)

        self.log.info('----备机1进行failover----')
        result = self.sta_sh.executeGsctl('failover',
                                          self.constant.FAILOVER_SUCCESS_MSG)
        self.assertTrue(result)
        self.log.info('----备机1进行refreshconf----')
        result = self.sta_sh.exec_refresh_conf()
        self.log.info(result)

        self.log.info('----kill主机数据库进程----')
        self.com.kill_pid_keyword('gaussdb', 9, self.pri_dbuser)
        time.sleep(2)

        self.log.info('----恢复主机数据库实例目录权限----')
        excute_cmd = f'chmod 700 {macro.DB_INSTANCE_PATH}'
        self.log.info(excute_cmd)
        msg = self.pri_dbuser.sh(excute_cmd).result()
        self.log.info(msg)
        self.assertNotIn(self.constant.NO_FILE_MSG, msg)
        time.sleep(2)

        self.log.info('----重启数据库----')
        result = self.pri_sh.stopDbCluster()
        self.assertTrue(result)
        result = self.pri_sh.startDbCluster()
        self.assertTrue(result)
        result = self.pri_sh.getDbClusterStatus('detail')
        self.log.info(result)

        self.log.info('----start tpcc----')
        result = self.com.startTPCC(self.sta_root, macro.TPCC_PATH, )
        self.log.info(result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, result)

        self.log.info('----查看主机query，同步是否正常----')
        self.sta_sh.check_location_consistency('primary',
                                               self.node_num,
                                               self.min_time)

        self.log.info('----查询主备状态----')
        status = self.sta_sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('----后置处理----')
        self.log.info('----恢复主机数据库实例目录权限----')
        excute_cmd = f'chmod 700 {macro.DB_INSTANCE_PATH}'
        self.log.info(excute_cmd)
        msg = self.pri_dbuser.sh(excute_cmd).result()
        self.log.info(msg)

        host_name = self.pri_root.sh('hostname').result()
        if self.pri_sh.check_whether_need_switch(host_name):
            self.log.info('----恢复主备关系----')
            if self.pri_sh.check_whether_need_build():
                self.log.info('如果备机无法恢复，需要重建备机')
                self.pri_sh.executeGsctl('build',
                                         self.constant.REBUILD_SUCCESS_MSG)
            self.log.info('---进行主备切换---')
            success_msg = self.constant.SWITCHOVER_SUCCESS_MSG
            result = self.pri_sh.executeGsctl('switchover', success_msg)
            self.log.info(result)
            result = self.pri_sh.exec_refresh_conf()
            self.log.info(result)
        else:
            self.log.info('----no need to switchover----')

        self.log.info('----Opengauss_Reliability_Dbsys_Case182 执行完成----')
